﻿/*
 * Autore: Artur Tolstenco
 */

using System;
using System.Globalization;
using RecipesManager.Data;

namespace RecipesManager.MainControls.RecipeSubControls
{
    public class StepFromRecipe
    {
        public static int BaseID;

        int _updatedId;

        public StepFromRecipe(IRecipeStep s) : this(s.Id, s.StepHours, s.StepMinutes, false)
        {
            Type = s.Type;
            if (Type == StepType.Generic)
            {
                Description = s.Description;
                Resources = s.Resources;
            }
            else
            {
                SubRecipeName = s.SubRecipe.Name;
                Servings = s.Servings;
                UpdateDescription();
            }
        }

        public StepFromRecipe(int id, string description, string resources, int? stepHours, 
                                int? stepMinutes, bool isNewStep = true)
            : this(id, stepHours, stepMinutes, isNewStep)
        {
            Type = StepType.Generic;
            Resources = resources;
            Description = description;

            //UpdateDescription();
        }

        public StepFromRecipe(int id, string subRecipeName, int? servings, int? stepHours,
                              int? stepMinutes, bool isNewStep = true)
            : this(id, stepHours, stepMinutes, isNewStep)
        {
            Type = StepType.SubRecipe;
            SubRecipeName = subRecipeName;
            Servings = servings;

            UpdateDescription();
        }

        public void UpdateDescription()
        {
            if (Type == StepType.SubRecipe)
                Description = "Esegui sottoricetta " + SubRecipeName + " per " + Servings + " porzioni";
            else
            {
                if (!Resources.Equals(""))
                    Description = Description + ";" + Environment.NewLine + "(Risorse occupate: " + Resources;
            }
        }

        StepFromRecipe(int id, int? stepHours, int? stepMinutes, bool isNewStep)
        {
            Id = id;
            UpdatedId = Id;
            IsNewStep = isNewStep;
            StepHours = stepHours;
            StepMinutes = stepMinutes;
        }

        public int Id { get; set; }

        public int UpdatedId
        {
            get { return _updatedId; }
            set { _updatedId = value; }
        }

        public string UpdatedIdStr
        {
            get
            {
                if (IsNewStep)
                    return _updatedId.ToString(CultureInfo.InvariantCulture);

                return (_updatedId - BaseID).ToString(CultureInfo.InvariantCulture);
            }
        }

        public string Description { get; set; }
        public StepType Type { get; set; }
        public string Resources { get; set; }
        public string SubRecipeName { get; set; }

        public int? Servings { get; set; }
        public int? StepHours { get; set; }
        public int? StepMinutes { get; set; }

        public bool IsNewStep { get; set; }

        /* must generate equals && hashcode */

        protected bool Equals(StepFromRecipe other)
        {
            return UpdatedId == other.UpdatedId;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((StepFromRecipe) obj);
        }

        public override int GetHashCode()
        {
            return UpdatedId;
        }
    }
}
